<?php
/* 
 * @package Analyser
 * @author Yves Peeters
 */

Class PageAnalyserDB extends DB_PDO {

    public function __construct() {
        parent::__construct();
    }
     /**
     * empty crawler_page table
     */
    public function initPagaAnalyser() {
        $query1=("truncate table crawler_page");
        $this->exec($query1);
        $query2=("truncate table crawler_meta");
        $this->exec($query2);
        $query3=("truncate table crawler_size");
        $this->exec($query3);

    }
    public function getSpiderIdForUrl($url){
        
        $query = "SELECT id FROM crawler where url = :url limit 1";
        $stmt= $this->prepare($query);
        $stmt->bindparam(':url',$url);
        $stmt->execute();
        $obj = $stmt->fetch(PDO::FETCH_OBJ);
        return  $obj->id;
    }

    public function getPageAnalysisForUrl($url){
        $id= $this->getSpiderIdForUrl($url);
        $query= "Select * from crawler_page where crawlid ='$id' limit 1";
        $stmt= $this->query($query);
        $obj = $stmt->fetch(PDO::FETCH_OBJ);
        while($obj = $stmt->fetch(PDO::FETCH_OBJ)){
            $return = $obj;
        }
        return $return;
    }

    public function insertPage($value){
        $query = "insert into crawler_page (crawlid,w3cvalid,html,plaintext,codecontentratio) 
        values (:crawlid,:w3cvalid,:html,:plaintext,:codecontentratio)";
        $stmt = $this->prepare($query);
        $stmt->bindparam(':crawlid',$value['crawlid']);
        $stmt->bindparam(':w3cvalid',$value['w3cvalid']);
        $stmt->bindparam(':html',$value['html']);
        $stmt->bindparam(':plaintext',$value['plaintext']);
        $stmt->bindparam(':codecontentratio',$value['codecontentratio']);
        $stmt->execute();
    }

    public function insertMeta($value){
        $query = "insert into crawler_meta (crawlid,description,keywords)
        values (:crawlid,:description,:keywords)";
        $stmt = $this->prepare($query);
        $stmt->bindparam(':crawlid',$value['crawlid']);
        $stmt->bindparam(':description',$value['description']);
        $stmt->bindparam(':keywords',$value['keywords']);
        $stmt->execute();
    }

    public function insertSize($value){
        $query = "insert into crawler_size (crawlid,html,image,css,js,total)
        values (:crawlid,:html,:image,:css,:js,:total)";
        $stmt = $this->prepare($query);
        $stmt->bindparam(':crawlid',$value['crawlid']);
        $stmt->bindparam(':html',$value['html']);
        $stmt->bindparam(':image',$value['image']);
        $stmt->bindparam(':css',$value['css']);
        $stmt->bindparam(':js',$value['js']);
        $stmt->bindparam(':total',$value['total']);
        $stmt->execute();        
    }

    public function insertSEO($SEO){
        $query = "insert into crawler_seo (crawlid,suggestedKeywords)
        values (:crawlid,:suggestedKeywords)";
        $stmt = $this->prepare($query);
        $stmt->bindparam(':crawlid',$SEO['crawlid']);
        $stmt->bindparam(':suggestedKeywords',$SEO['suggestedKeywords']);
        $stmt->execute();
    }

}
?>
